// calcolo del percorso

var directionDisplay;
var directionsService = new google.maps.DirectionsService();



function initialize_traceroute() {
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsDisplay.setMap(gMap);
	directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}

function setPartenzaArrivo(){
	var partenza = document.querySelector('#partenza');
	var arrivo = document.querySelector('#arrivo');
	
	var indirizzo_geo = codeLatLng(MYlatlng);	
	if(indirizzo_geo) partenza.value = indirizzo_geo;
	
	var indirizzo_part = codeLatLng(target);	
	if(indirizzo_part) arrivo.value = indirizzo_part;
	
	
}


function calcRoute() {
	if(!target) return;
//	setPartenzaArrivo();
	var selectedMode = document.getElementById("mode").value;
	var request = {
		origin: MYlatlng,
		destination: target,
		// Note that Javascript allows us to access the constant
		// using square brackets and a string value as its
		// "property."
		travelMode: google.maps.TravelMode[selectedMode]
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
			computeTotalDistance(directionsDisplay.directions);
		}
	});
}

function computeTotalDistance(result) {
	var total = 0;
	var myroute = result.routes[0];
	for (i = 0; i < myroute.legs.length; i++) {
		total += myroute.legs[i].distance.value;
	}
	total = total / 1000.
	document.getElementById("total").innerHTML = "Distanza: " +total + " km";
} 